package cn.wolfcode.luowowo.controller;

import cn.wolfcode.luowowo.domain.StrategyCatalog;
import cn.wolfcode.luowowo.query.StrategyCatalogQuery;
import cn.wolfcode.luowowo.service.IStrategyCatalogService;
import cn.wolfcode.luowowo.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

//攻略分类控制器
@Controller
@RequestMapping("strategyCatalog")
public class StrategyCatalogController {
    //注入攻略分类业务
    @Autowired
    private IStrategyCatalogService strategyCatalogService;
    //攻略分类分页查询
    @RequestMapping("/list")
    public String list(Model model , @ModelAttribute("qo") StrategyCatalogQuery qo){
        //Page是spring-data-mongodb 里封装分页参数的对象.类似pegeinfo
        Page<StrategyCatalog> page = strategyCatalogService.list(qo);
        model.addAttribute("page" , page);
        return "strategyCatalog/list";
    }

    //新增/编辑
    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public Object saveOrUpdate(StrategyCatalog strategyCatalog){
        if (StringUtils.hasLength(strategyCatalog.getId())){
            //带id则是编辑
            strategyCatalogService.update(strategyCatalog);
        }else {
            //没带则是新增
            strategyCatalogService.save(strategyCatalog);
        }
        //返回结果状态
        return JsonResult.success();
    }

    //删除
    @RequestMapping("/delete")
    @ResponseBody
    public Object delete(String id){
        strategyCatalogService.delete(id);
        //返回结果状态
        return JsonResult.success();
    }

}
